package com.treehouse.url;

import java.net.URLDecoder;

public class HTMLParser {
	
	public HTMLParser(){
		
	}
	
	public String decodeHTML(String html){
			String data = html;
			  try {
			     StringBuffer tempBuffer = new StringBuffer();
			     int incrementor = 0;
			     int dataLength = data.length();
			     while (incrementor < dataLength) {
			        char charecterAt = data.charAt(incrementor);
			        if (charecterAt == '%') {
			           tempBuffer.append("<percentage>");
			        } else if (charecterAt == '+') {
			           tempBuffer.append("<plus>");
			        } else {
			           tempBuffer.append(charecterAt);
			        }
			        incrementor++;
			     }
			     data = tempBuffer.toString();
			     data = URLDecoder.decode(data, "utf-8");
			     data = data.replaceAll("<percentage>", "%");
			     data = data.replaceAll("<plus>", "+");
			     data = data.replaceAll("amp;", "");
			     data = data.replaceAll("\\\\", "");
			  } catch (Exception e) {
			     e.printStackTrace();
			  }
			  return data;
	}
	
}
